package com.jingzhi.count.service;

import com.jingzhi.count.entity.Team;
import com.jingzhi.count.mapper.TeamMapper;
import com.jingzhi.count.model.Paging;
import com.jingzhi.count.utils.JWTUtil;
import com.jingzhi.count.utils.TimeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Team Service
 * Created by cmw on 2018/4/8.
 */
@Service
public class TeamService {

    @Autowired
    private TeamMapper teamMapper;

    /**
     * 登陆
     * @param teamIn
     * @return
     */
    public String login(Team teamIn) {
        String rs = "";
        try {
            UsernamePasswordToken uToken = new UsernamePasswordToken(teamIn.getAccount(), teamIn.getPassword());
            Subject subject = SecurityUtils.getSubject();
            Team team = teamMapper.selectByAccount(teamIn.getAccount());
            if (team != null) {
                subject.login(uToken);
                String token = JWTUtil.sign(team.getAccount());
                teamIn.setToken(token);
            } else {
                rs = "用户不存在";
            }
        } catch (AuthenticationException authenticationException) {
            rs = "用户名或密码不正确";
        } catch (Exception ex) {
            rs = "系统异常请联系管理员";
        }
        return rs;
    }
    /**
     * 分页查询
     * @return
     */
    public void selectAllPage( Paging<Team> paging) {
        paging.setTotal(teamMapper.selectAllPageCount(paging.getSearchMap()));
        paging.setData(teamMapper.selectAllPageList(paging.getSearchMap()));
    }
}
